#include "EventReceiver.h"

EventReceiver::EventReceiver() {
	JoystickActive = false;
	for (u32 key = 0; key < KEY_KEY_CODES_COUNT; ++key) {
		KeyIsClicked[key] = false;
		KeyIsDown[key] = false;
	}
}     
void EventReceiver::ActivateJoysticks(IrrlichtDevice *Device) {
	if(Device->activateJoysticks(JoystickInfo)) {
		if(JoystickInfo.size() > 0) {
			JoystickActive = true;
			SetNumberOfJoysticks(JoystickInfo.size());

			for(u32 joystick = 0; joystick < JoystickInfo.size(); ++joystick) {
				cout << "Joystick: " << joystick << endl;
				cout << "\tName: " << JoystickInfo[joystick].Name.c_str() << endl;
				cout << "\tAxes: " << JoystickInfo[joystick].Axes << endl;
				cout << "\tButtons: " << JoystickInfo[joystick].Buttons << endl;
				cout << "\tHat is: ";

				switch(JoystickInfo[joystick].PovHat) {
					case SJoystickInfo::POV_HAT_PRESENT:
						cout << "Present" << endl;
						break;

					case SJoystickInfo::POV_HAT_ABSENT:
						cout << "Absent" << endl;
						break;

					case SJoystickInfo::POV_HAT_UNKNOWN:
						default:
						cout << "Unknown" << endl;
						break;
				}
			}
			const SEvent::SJoystickEvent &joystickData = GetJoystickState(0);
			SetButtonClickArray(0, JoystickInfo);
		}
	}
}
bool EventReceiver::OnEvent(const SEvent &event) {
    if(event.EventType == EET_MOUSE_INPUT_EVENT) {
		switch(event.MouseInput.Event) {
			case EMIE_LMOUSE_DOUBLE_CLICK:
				MouseState.LeftButtonfloatClicked = true;
				break;

            case EMIE_LMOUSE_LEFT_UP:
                MouseState.LeftButtonDown = false;
				MouseState.LeftButtonfloatClicked = false;
                break;

			case EMIE_LMOUSE_PRESSED_DOWN:
				MouseState.LeftButtonDown = true;
                break;

			case EMIE_RMOUSE_DOUBLE_CLICK:
				MouseState.RightButtonfloatClicked = true;
				break;

            case EMIE_RMOUSE_LEFT_UP:
                MouseState.RightButtonDown = false;
				MouseState.RightButtonfloatClicked = false;
                break;

			case EMIE_RMOUSE_PRESSED_DOWN:
				MouseState.RightButtonDown = true;
                break;

            case EMIE_MOUSE_MOVED:
                MouseState.Position.X = event.MouseInput.X;
                MouseState.Position.Y = event.MouseInput.Y;
                break;

			case EMIE_MOUSE_WHEEL:
				MouseState.WheelValue = event.MouseInput.Wheel;
				break;

            default:
                break;
		}
	}

    if (event.EventType == EET_KEY_INPUT_EVENT)
		KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

    if (event.EventType == EET_JOYSTICK_INPUT_EVENT)
		JoystickStates[event.JoystickEvent.Joystick] = event.JoystickEvent;
               
	return false;
}
const bool EventReceiver::IsButtonClicked(int Joystick_Number, int Button_Number) {
	if(Joystick_Number == 0) {
		if(GetJoystickState(Joystick_Number).IsButtonPressed(Button_Number) == true && JoystickStateClicked1[Button_Number] == false) {
			JoystickStateClicked1[Button_Number] = true;
			return true;
		}
		else if(GetJoystickState(Joystick_Number).IsButtonPressed(Button_Number) == false) {
			JoystickStateClicked1[Button_Number] = false;
			return false;
		}
		else
			return false;
	}
	else {
		if(GetJoystickState(Joystick_Number).IsButtonPressed(Button_Number) == true && JoystickStateClicked2[Button_Number] == false) {
			JoystickStateClicked2[Button_Number] = true;
			return true;
		}
		else if(GetJoystickState(Joystick_Number).IsButtonPressed(Button_Number) == false) {
			JoystickStateClicked2[Button_Number] = false;
			return false;
		}
		else
			return false;
	}
}
const bool EventReceiver::IsJoystickActive() {
	return JoystickActive;
}
const bool EventReceiver::IsKeyClicked(EKEY_CODE Key_Code) {
	if(KeyIsDown[Key_Code] == true && KeyIsClicked[Key_Code] == false) {
		KeyIsClicked[Key_Code] = true;
		return true;
	}
	else if(KeyIsDown[Key_Code] == false && KeyIsClicked[Key_Code] == true) {
		KeyIsClicked[Key_Code] = false;
		return false;
	}
	else
		return false;
}
const bool EventReceiver::IsKeyDown(EKEY_CODE Key_Code) {
	return KeyIsDown[Key_Code];
}
void EventReceiver::SetButtonClickArray(int Joystick_Number, array<SJoystickInfo> Joystick_Info) {
	for(u32 count = 0; count < Joystick_Info[Joystick_Number].Buttons; count ++) {
		JoystickStateClicked1.push_back(false);
		JoystickStateClicked2.push_back(false);
	}
}
void EventReceiver::SetNumberOfJoysticks(int Number) {
	JoystickStates.resize(Number);
}